package cn.edu.scau.cmi.chenmiaopeng.spring.client;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.context.ApplicationContext;
import cn.edu.scau.cmi.chenmiaopeng.spring.util.ComprehensionApplicationContextUtil;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.Tooltip;
import javafx.stage.Stage;
import cn.edu.scau.cmi.chenmiaopeng.hibernate.dao.*;
import cn.edu.scau.cmi.chenmiaopeng.hibernate.domain.*;
public class SpringHibernateJavaFxClient extends Application{
	StudentDAO studentDAO;
	TeacherDAO teacherDAO;
    ApplicationContext applicationContext;
	
	public static void main(String[] args) {
		launch(args);                                
	}
	public void start(Stage primaryStage) throws Exception{
		try {
		  FXMLLoader loader = new FXMLLoader(getClass().getResource("/cn/edu/scau/cmi/chenmiaopeng/ui/Spring/SpringHibernateUI.fxml"));
		  Parent root = loader.load();
		  Scene scene = new Scene(root, 470, 460);
		  primaryStage.setScene(scene);
		  primaryStage.setTitle("spring框架基础");
		  initView(loader);
		  primaryStage.show();
		}
		catch(IOException e) {
			e.printStackTrace();
		}
	}
	public void initView(FXMLLoader loader) {
		TextArea textArea = (TextArea)loader.getNamespace().get("textArea");
		Button button1 = (Button)loader.getNamespace().get("confirm1");
		Tooltip tooltip2 = new Tooltip("点击显示导师LiangZaoqing和指导学生Zhangsan。");
		button1.setTooltip(tooltip2);
		
        button1.setOnAction(new EventHandler<ActionEvent>() {
            
            @Override
            public void handle(ActionEvent event) {
            	String info = getInfoBeforeDebate();
            	textArea.setText(info);
            }
        });

		Button button2 = (Button)loader.getNamespace().get("confirm2");
		Tooltip tooltip1 = new Tooltip("点击添加导师和指导学生。");
		button2.setTooltip(tooltip1);
		
		formInstructRelation();
		
        button2.setOnAction(new EventHandler<ActionEvent>() {
            
            @Override
            public void handle(ActionEvent event) {
            	String info = getStringTeacherStudent();
            	textArea.setText(info);
            }
        });
		
		
	}
	private void formInstructRelation() {
		applicationContext = ComprehensionApplicationContextUtil.getApplicationContext();
		studentDAO=(StudentDAO) applicationContext.getBean("studentDAO");
		teacherDAO=(TeacherDAO) applicationContext.getBean("teacherDAO");
//		Teacher liang = teacherDAO.findById(5L); if(liang!=null)
//		Student chen = studentDAO.findById(6L);  if(chen!=null) 
		Teacher LiangZaoqing = new Teacher(5L,"LiangZaoqing");
		Student chenmiaopeng = new Student(6L,"ChenMiaopeng");
		teacherDAO.delete(LiangZaoqing);
		studentDAO.delete(chenmiaopeng);
		teacherDAO.save(LiangZaoqing);
		chenmiaopeng.setTeacher(LiangZaoqing);
		studentDAO.save(chenmiaopeng);
	}
	private String getInfoBeforeDebate() {
		StringBuffer sb = new StringBuffer();
		Student student = (Student) applicationContext.getBean("Zhangsan");
//		student.setId(5L);
//		student.setName("Zhangsan");
//		Teacher teacher = (Teacher)applicationContext.getBean("LiangZaoqing");
//		teacher.setId(6L);
//		teacher.setName("LiangZaoqing");
		sb.append("学号："+student.getId()+"    姓名："+student.getName() + "的毕业设计指导导师是:\n " + "--工号："+student.getTeacher().getId()+"    姓名："+student.getTeacher().getName());
		return new String(sb);
	}
	private String getStringTeacherStudent() {
        StringBuffer sb = new StringBuffer();
		Teacher teacher = teacherDAO.findById(5L);
		sb.append("工号："+teacher.getId()+"    导师姓名："+teacher.getName()+"\n");
		Set<Student> stus = teacher.getStudents();
		Iterator<Student> instruct_stus = stus.iterator();
		   while(instruct_stus.hasNext()) {
				Student stu = instruct_stus.next();
				if(stu.getName().equals("ChenMiaopeng")) {
						sb.append("--指导的学生是：学号："+stu.getId()+"    姓名："+stu.getName());
				}
	       }
		   return new String(sb);
  }
}
